package cn.mesmile.grpc.service.impl;

import cn.mesmile.grpc.demo.Greeting;
import cn.mesmile.grpc.demo.HelloWorldServiceGrpc;
import cn.mesmile.grpc.demo.Person;
import com.google.protobuf.ProtocolStringList;
import io.grpc.stub.StreamObserver;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import org.lognet.springboot.grpc.GRpcService;

/**
 * @author zb
 * @date 2021/1/3 11:46
 * @Description: 创建服务器
 */
@Slf4j
@GRpcService
public class HelloWorldServiceImpl extends HelloWorldServiceGrpc.HelloWorldServiceImplBase {


    @Override
    public void sayHello(Person request, StreamObserver<Greeting> responseObserver) {

        log.info("server received {}", request);

        ProtocolStringList nameListList = request.getNameListList();
        StringBuilder sb = new StringBuilder();
        for (String s : nameListList) {
            sb.append(s);
        }

        String message = "Hello " + request.getFirstName() + " " + request.getLastName()+ " "+ sb.toString() +"!";

        Greeting greeting = Greeting.newBuilder().setMessage(message)
//                .addAllNameResp(nameListList)
                .build();
        log.info("server responded {}", greeting);

        responseObserver.onNext(greeting);
        responseObserver.onCompleted();
    }

}
